package db.tables;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

import com.db.Table;

import db.beans.M_DictionaryBean;

public class M_DictionaryTable extends Table {
	public static final String TABLE_NAME = "M_DICTIONARY";
	public static final String COL_DICTIONARY_ID = "DICTIONARY_ID";
	public static final String COL_DICTIONARY_NAME = "DICTIONARY_NAME";
	public static final String COL_LANGUAGE_ID = "LANGUAGE_ID";
	public static final String COL_TABLE_NAME = "DICTIONARY_TABLE_NAME";
	public static final String COL_DISCREPTION = "DISCREPTION";
	public static final String COL_DELETE_FLAG = "DELETE_FLAG";
	public static final String F_COL_DICTIONARY_ID = TABLE_NAME + "." + "DICTIONARY_ID";
	public static final String F_COL_DICTIONARY_NAME = TABLE_NAME + "." + "DICTIONARY_NAME";
	public static final String F_COL_LANGUAGE_ID = TABLE_NAME + "." + "LANGUAGE_ID";
	public static final String F_COL_TABLE_NAME = TABLE_NAME + "." + "DICTIONARY_TABLE_NAME";
	public static final String F_COL_DISCREPTION = TABLE_NAME + "." + "DISCREPTION";
	public static final String F_COL_DELETE_FLAG = TABLE_NAME + "." + "DELETE_FLAG";
	public static final String ALL_COL =
		COL_DICTIONARY_ID
		+ "," + COL_DICTIONARY_NAME
		+ "," + COL_LANGUAGE_ID
		+ "," + COL_TABLE_NAME
		+ "," + COL_DISCREPTION
		+ "," + COL_DELETE_FLAG
		;

	public static M_DictionaryBean[] selectAll(Connection conn) throws SQLException {
		final String sql = "SELECT "
				+ ALL_COL
				+ " FROM " + TABLE_NAME;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		Vector<M_DictionaryBean> v = new Vector<M_DictionaryBean>();
		try {
			stmt = conn.prepareStatement(sql);
			rs = stmt.executeQuery();
			int i;
			while (rs.next()) {
				i = 0;
				M_DictionaryBean rec = new M_DictionaryBean();
				rec.setDictionaryId(rs.getInt(++i));
				rec.setDictionaryName(rs.getString(++i));
				rec.setLanguageId(rs.getInt(++i));
				rec.setTableName(rs.getString(++i));
				rec.setDiscreption(rs.getString(++i));
				rec.setDeleteFlag(rs.getInt(++i) == DELETE_FLAG);
				v.add(rec);
			}
		} finally {
			if (rs != null) {
				rs.close();
			}

			if (stmt != null) {
				stmt.close();
			}
		}
		return (M_DictionaryBean[]) v.toArray(new M_DictionaryBean[v.size()]);
	}

	public static String[] selectDictionaryTableNameByLanguageId(Connection conn, int languageId) throws SQLException {
		final String sql = "SELECT "
				+ COL_TABLE_NAME
				+ " FROM " + TABLE_NAME
				+ " WHERE " + COL_LANGUAGE_ID + "=?"
				;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		Vector<String> v = new Vector<String>();
		try {
			stmt = conn.prepareStatement(sql);
			int i = 0;
			stmt.setInt(++i, languageId);
			rs = stmt.executeQuery();
			while (rs.next()) {
				i = 0;
				v.add(rs.getString(++i));
			}
		} finally {
			if (rs != null) {
				rs.close();
			}

			if (stmt != null) {
				stmt.close();
			}
		}
		return (String[]) v.toArray(new String[v.size()]);
	}
}
